<?php


namespace app\api\controller\xiluedu;

use app\common\controller\Api;
use think\Db;
use think\Exception;
use app\common\model\xiluedu\CourseComment AS CourseCommentModel;

class CourseComment extends Api {

    protected $noNeedLogin = ['lists'];
    protected $noNeedRight = ['*'];
    /**
     * 发布评论
     */
    public function add_comment() {
        $params = $this->request->post('');
        $params['user_id'] = $this->auth->id;
        Db::startTrans();
        try {
            $ret = CourseCommentModel::addComment($params);
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        Db::commit();
        $this->success('评论成功', $ret);

    }

    /**
     * 我发布的评论
     */
    public function my_comment() {
        $pagesize = $this->request->param('pagesize',10);
        $list = CourseCommentModel::where('course_comment.user_id', $this->auth->id)
            ->with([
                'user'=>function($query){$query->withField('id,avatar,nickname');},
                //'course'=>function($query){$query->withField('id,name,thumb_image');},
            ])
            ->order('id','desc')
            ->paginate($pagesize);
        $this->success('查询成功',$list);
    }

    /**
     * 评价列表
     * @throws \think\exception\DbException
     */
    public function lists() {
        $course_id = $this->request->param('course_id',null);
        $list = CourseCommentModel::lists($course_id);
        $this->success('', $list);

    }

}